import VueRouter from "vue-router";
import GetAsyncComponent from '@/libs/utils/async'
import { defaultLang, changeLocale } from '@/i18n'
Vue.use(VueRouter);

export const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import(/* webpackChunkName: "home" */ "../views/Home.vue"),
    meta: {
      title: "首页"
    }
  },
  {
    path: "/skeleton",
    name: "Skeleton",
    component: () => import(/* webpackChunkName: "virtual" */ "../views/Skeleton.vue"),
    meta: {
      title: "骨架屏",
      keyWords: "骨架屏，Vue骨架屏",
      description: '骨架屏描述'
    }
  },
  // {
  //   path: "/about",
  //   name: "About",
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ "../views/About.vue"),
  // },
  {
    path: "/loading",
    name: "Loading",
    component: () => import(/* webpackChunkName: "loading" */ "../views/Loading.vue"),
    meta: {
      title: "v-loading效果"
    }
  },
  {
    path: "/lazy",
    name: "Lazy",
    component: () => import(/* webpackChunkName: "lazy" */ "../views/Lazy.vue"),
    meta: {
      title: "图片懒加载"
    }
  },
  {
    path: "/async",
    name: "Async",
    component: GetAsyncComponent(() => import(/* webpackChunkName: "async" */ "../views/Async/index.vue")),
    meta: {
      title: "异步组件和路由"
    }
  },
  {
    path: "/pull",
    name: "PullRefresh",
    component: () => import(/* webpackChunkName: "pull" */ "../views/PullRefresh.vue"),
    meta: {
      title: "下拉刷新"
    }
  },
  {
    path: "/infinite",
    name: "InfiniteScroll",
    component: () => import(/* webpackChunkName: "infinite" */ "../views/InfiniteScroll.vue"),
    meta: {
      title: "上拉加载",
    }
  },
  {
    path: "/virtual",
    name: "VirtualScroll",
    component: () => import(/* webpackChunkName: "virtual" */ "../views/VirtualScroll.vue"),
    meta: {
      title: "虚拟滚动",
      keepAlive: true
    }
  },
  {
    path: "/websocket",
    name: "WebSocket",
    component: () => import(/* webpackChunkName: "websocket" */ "../views/Socket.vue"),
    meta: {
      title: "WebSocket"
    }
  },
  {
    path: "/i18n",
    name: "I18n",
    component: () => import(/* webpackChunkName: "i18n" */ "../views/I18n.vue"),
    meta: {
      i18nTitle: "i18n"
    }
  },
  {
    path: "/call",
    name: "Call",
    component: () => import(/* webpackChunkName: "call" */ "../views/Call.vue"),
    meta: {
      title: "APP唤醒"
    }
  },
  {
    path: "/throttle",
    name: "Throttle",
    component: () => import(/* webpackChunkName: "throttle" */ "../views/Throttle.vue"),
    meta: {
      title: "点击防抖"
    }
  },
  {
    path: "/copy",
    name: "Copy",
    component: () => import(/* webpackChunkName: "copy" */ "../views/Copy.vue"),
    meta: {
      title: "一键复制"
    }
  },
  {
    path: "/dark",
    name: "Dark",
    component: () => import(/* webpackChunkName: "dark" */ "../views/Dark.vue"),
    meta: {
      title: "黑夜模式"
    }
  },
  {
    path: "/persist",
    name: "Persist",
    component: () => import(/* webpackChunkName: "persist" */ "../views/Persist.vue"),
    meta: {
      title: "持久缓存"
    }
  },
  {
    path: "/vconsole",
    name: "VConsole",
    component: () => import(/* webpackChunkName: "vconsole" */ "../views/VConsole.vue"),
    meta: {
      title: "控制台调试"
    }
  },
];

// vue相同路由跳转报错问题处理
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if(onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
console.log('BASE_URL', process.env.BASE_URL)
console.log('defaultLang', defaultLang)
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL + defaultLang,
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition && to.meta.keepAlive) {
    return savedPosition;
    }
    return { x: 0, y: 0 };
  }
});
// 这里可以做路由拦截
router.beforeEach((to, from, next) => {
  changeLocale(to.meta)
  next()
})

export default router;
